﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Waker.Common;
using Waker.Entitys;
using Waker.APIs.ADS;

namespace Waker.Cards
{
    public partial class FormSubContractorEdit : Waker.Base.FormBasePop
    {
        DataTable _DtSource;
        public FormSubContractorEdit(string id)
        {
            InitializeComponent();
            this.Text += string.IsNullOrEmpty(id) ? "添加" : "修改";
            _DtSource = new BAL.BAL_Cards().GetDepartmentById(id);
            this.Load += new EventHandler(FormSubContractorEdit_Load);
        }

        private void FormSubContractorEdit_Load(object sender, EventArgs e)
        {
            if (_DtSource.Rows.Count < 1)
            {
                this.BindingContext[_DtSource].AddNew();
                txtDept_Code.Enabled = true;
            }
            this.BindingContext[_DtSource].EndCurrentEdit();
            ControlUtil.BindData(this.Controls, _DtSource);
            var dtBaseData = new BAL.BAL_SystemSetting().GetAllBaseDataByDataType(Enums.BaseDataType.ConstructionType);
            ControlUtil.FillLookUpEdit(dtBaseData, lueDept_TypeId, "DataName", "ID");
            var dtSite = new BAL.BAL_Cards().GetAllConstructionSite();
            ControlUtil.FillLookUpEdit(dtSite, lueSite_ID, "Site_Name", "Site_ID");
        }
        protected override void FormSave()
        {
            if (_DtSource == null || _DtSource.Rows.Count < 1)
                return;
            base.errorPop.ClearErrors();
            if (string.IsNullOrEmpty(txtDept_Code.Text))
            {
                base.errorPop.SetError(txtDept_Code, "编号不能为空");
                return;
            }
            if (string.IsNullOrEmpty(txtDept_Name.Text))
            {
                base.errorPop.SetError(txtDept_Name, "名称不能为空");
                return;
            }
            if (lueDept_TypeId.EditValue == DBNull.Value || lueDept_TypeId.EditValue == null)
            {
                base.errorPop.SetError(lueDept_TypeId, "请选择类型");
                return;
            }
            if (lueSite_ID.EditValue == DBNull.Value || lueSite_ID.EditValue == null)
            {
                base.errorPop.SetError(lueSite_ID, "请选择工地");
                return;
            }
            this.BindingContext[_DtSource].EndCurrentEdit();
            if (_DtSource == null || _DtSource.Rows.Count < 1)
                return;
            //上传部门到设备上
            //ADSHalDataStruct.ADS_Department dept = new ADSHalDataStruct.ADS_Department();
            //dept.superiorID = (byte)(Convert.ToUInt16(0));
            //dept.ID = (byte)(Convert.ToUInt16(textBox11.Text));
            //int iResult = ADSHalAPI.ADS_SetDepartment(ref m_comAdatpter, ref m_comm, ref dept);
            //ADSHelp.PromptResult(iResult, true);
            var isOk = new BAL.BAL_Cards().SaveDepartment(_DtSource.Rows[0]);
            if (isOk)
            {
                UI.ShowInfo("保存成功");
                this.DialogResult = DialogResult.OK;
            }
            else
            {
                UI.ShowError("保存失败");
            }
        }
    }
}
